//
//  BeginTimeAndTimeOffsetAndSpeedViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/28.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "BeginTimeAndTimeOffsetAndSpeedViewController.h"
#import "UIDevice+XJDevice.h"

@interface BeginTimeAndTimeOffsetAndSpeedViewController ()<CAAnimationDelegate>

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) UIBezierPath *bezierPath;

@property (nonatomic, strong) CALayer *imageLayer;

@property (nonatomic, strong) UISlider *changeSpeedSlider;

@property (nonatomic, strong) UISlider *changeTimeOffsetSlider;

@property (nonatomic, strong) UISlider *changeBeginTimeSlider;

@property (nonatomic, strong) UIButton *playButton;

@property (nonatomic, strong) UIButton *returnButton;

@property (nonatomic, strong) UILabel *showSpeedLabel;

@property (nonatomic, strong) UILabel *showTimeOffsetLabel;

@property (nonatomic, strong) UILabel *showBeginTimeLabel;

@end

@implementation BeginTimeAndTimeOffsetAndSpeedViewController

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIDevice setOrientation:UIInterfaceOrientationPortrait];
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

/**
 *  beginTime指定了动画开始之前的的延迟时间。这里的延迟从动画添加到可见图层的那一刻开始测量，默认是0（就是说动画会立刻执行）。
 
 speed是一个时间的倍数，默认1.0，减少它会减慢图层/动画的时间，增加它会加快速度。如果2.0的速度，那么对于一个duration为1的动画，实际上在0.5秒的时候就已经完成了。
 
 timeOffset和beginTime类似，但是和增加beginTime导致的延迟动画不同，增加timeOffset只是让动画快进到某一点，例如，对于一个持续1秒的动画来说，设置timeOffset为0.5意味着动画将从一半的地方开始。
 timeOffset只和动画的duration有关系，比如duration=1.0，speed=2,timeOffset=0.5，那么动画会从中间开始，但是执行时间变为0.5秒(因为速度为2)
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
    self.backgroundView.backgroundColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1];
    [self.view addSubview:_backgroundView];
    
    // 创建贝塞尔
    self.bezierPath = [UIBezierPath bezierPath];
    [self.bezierPath moveToPoint:CGPointMake(100, 200)];
    [self.bezierPath addCurveToPoint:CGPointMake(500, 200) controlPoint1:CGPointMake(200, 50) controlPoint2:CGPointMake(400, 350)];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = self.bezierPath.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineWidth = 4.0f;
    shapeLayer.lineCap = kCALineCapRound;
    [self.backgroundView.layer addSublayer:shapeLayer];
    
    // 箭头
    self.imageLayer = [CALayer layer];
    self.imageLayer.bounds = CGRectMake(0, 0, 30, 30);
    self.imageLayer.position = CGPointMake(100, 200);
    self.imageLayer.contents = (__bridge id _Nullable)([UIImage imageWithName:@"蒙版2"].CGImage);
    [self.backgroundView.layer addSublayer:_imageLayer];
    
    UILabel *beginTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 280, 100, 30)];
    beginTimeLabel.text = @"beginTime";
    [self.backgroundView addSubview:beginTimeLabel];
    
    UILabel *speedLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 310, 100, 30)];
    speedLabel.text = @"speed";
    [self.backgroundView addSubview:speedLabel];
    
    UILabel *timeOffsetLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 340, 100, 30)];
    timeOffsetLabel.text = @"timeOffset";
    [self.backgroundView addSubview:timeOffsetLabel];
    
    self.changeBeginTimeSlider = [[UISlider alloc] initWithFrame:CGRectMake(150, 280, 250, 30)];
    self.changeBeginTimeSlider.minimumValue = 0.0f;
    self.changeBeginTimeSlider.maximumValue = 1.0f;
    [self.changeBeginTimeSlider addTarget:self action:@selector(changeBeginTimeSlider:) forControlEvents:UIControlEventValueChanged];
    [self.backgroundView addSubview:_changeBeginTimeSlider];
    
    self.changeSpeedSlider = [[UISlider alloc] initWithFrame:CGRectMake(150, 310, 250, 30)];
    self.changeSpeedSlider.minimumValue = 0.0f;
    self.changeSpeedSlider.maximumValue = 2.0f;
    self.changeSpeedSlider.value = 1.0f;
    [self.changeSpeedSlider addTarget:self action:@selector(changeSpeedSlider:) forControlEvents:UIControlEventValueChanged];
    [self.backgroundView addSubview:_changeSpeedSlider];
    
    self.changeTimeOffsetSlider = [[UISlider alloc] initWithFrame:CGRectMake(150, 340, 250, 30)];
    self.changeTimeOffsetSlider.minimumValue = 0.0f;
    self.changeTimeOffsetSlider.maximumValue = 1.0f;
    [self.changeTimeOffsetSlider addTarget:self action:@selector(changeTimeOffsetSlider:) forControlEvents:UIControlEventValueChanged];
    [self.backgroundView addSubview:_changeTimeOffsetSlider];
    
    self.showBeginTimeLabel = [[UILabel alloc] initWithFrame: CGRectMake(420, 280, 50, 30)];
    self.showBeginTimeLabel.backgroundColor = [UIColor whiteColor];
    self.showBeginTimeLabel.text = [NSString stringWithFormat:@"%0.2f", self.changeBeginTimeSlider.value];
    [self.backgroundView addSubview:_showBeginTimeLabel];
    
    self.showSpeedLabel = [[UILabel alloc] initWithFrame: CGRectMake(420, 310, 50, 30)];
    self.showSpeedLabel.backgroundColor = [UIColor whiteColor];
    self.showSpeedLabel.text = [NSString stringWithFormat:@"%0.2f", self.changeSpeedSlider.value];
    [self.backgroundView addSubview:_showSpeedLabel];
    
    self.showTimeOffsetLabel = [[UILabel alloc] initWithFrame: CGRectMake(420, 340, 50, 30)];
    self.showTimeOffsetLabel.backgroundColor = [UIColor whiteColor];
    self.showTimeOffsetLabel.text = [NSString stringWithFormat:@"%0.2f", self.changeTimeOffsetSlider.value];
    [self.backgroundView addSubview:_showTimeOffsetLabel];
    
    // 开始动画
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.frame = CGRectMake(500, 300, 50, 50);
    self.playButton.backgroundColor = [UIColor whiteColor];
    [self.playButton setTitle:@"play" forState:UIControlStateNormal];
    [self.playButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:_playButton];
    
    self.returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.returnButton.frame = CGRectMake(30, 10, 100, 30);
    self.returnButton.backgroundColor = [UIColor whiteColor];
    [self.returnButton setTitle:@"returnBack" forState:UIControlStateNormal];
    [self.returnButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.returnButton addTarget:self action:@selector(returnBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:_returnButton];
}

// 改变开始时间
- (void)changeBeginTimeSlider:(UISlider *)slider
{
    self.showBeginTimeLabel.text = [NSString stringWithFormat:@"%0.2f", slider.value];
}

// 改变动画的速度
- (void)changeSpeedSlider:(UISlider *)slider
{
    self.showSpeedLabel.text = [NSString stringWithFormat:@"%0.2f", slider.value];
}

// 改变动画的快进速度
- (void)changeTimeOffsetSlider:(UISlider *)slider
{
    self.showTimeOffsetLabel.text = [NSString stringWithFormat:@"%0.2f", slider.value];
}

- (void)play:(UIButton *)button
{
    [self setEnabled:NO];
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.path = self.bezierPath.CGPath;
    keyAnimation.duration = 1.0;
    keyAnimation.speed = self.changeSpeedSlider.value;
    keyAnimation.timeOffset = self.changeTimeOffsetSlider.value;
    keyAnimation.beginTime = self.changeBeginTimeSlider.value; // beginTime不为0的时候动画不会走
    keyAnimation.rotationMode = kCAAnimationRotateAuto;
    keyAnimation.delegate = self;
    [self.imageLayer addAnimation:keyAnimation forKey:nil];
    
}

- (void)returnBack:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)animationDidStop:(CAKeyframeAnimation *)anim finished:(BOOL)flag
{
    [self setEnabled:YES];
}

- (void)setEnabled:(BOOL)Enabled
{
        for (UIControl *control in @[self.playButton]) {
            control.enabled = Enabled;
            if (Enabled) {
                control.alpha = 1.0;
            } else {
                 control.alpha = 0.3;
            }
           
        }
    
}

@end
